import { ref } from "vue";
import { useStore } from "vuex";
/**
 * 上传头像请求
 * @returns {{isLoading: Ref<UnwrapRef<boolean>, UnwrapRef<boolean> | boolean>, handleAvatarChange: ((function(*, *): Promise<void>)|*)}}
 */
export function useTaskFileUpload(serviceData) {
    const isLoading = ref(false); // 加载状态
    const store = useStore(); // 获取 Vuex Store 实例

    // 处理文件上传
    const handleUploadTaskFile = async (event) => {
        const file = event.target.files[0];
        if (file) {
            const taskFile = new FormData(); // 创建 FormData 实例
            taskFile.append("file", file);
            isLoading.value = true; // 设置加载状态为 true
            try {
                // 调用 Vuex 中的 updateTaskFile action
                const response = await store.dispatch("updateTaskFile", taskFile);

                if (response.success) {
                    serviceData.value.attachmentUrl = response.data; // 更新前端展示的简历
                    serviceData.value.attachmentName = file.name;
                    console.log(111,serviceData)
                } else {
                    alert(response.message || "附件上传失败");
                }
            } catch (error) {
                console.error("上传附件时出错:", error);
                alert("上传失败，请稍后重试");
            } finally {
                isLoading.value = false; // 重置加载状态
            }
        }
    };

    return {
        handleUploadTaskFile,
        isLoading,
    };
}
